<template>
  <div class="comment-manage">
    <!-- 搜索区域 -->
    <div class="filter-area mb-5">
      <div class="flex flex-wrap items-center gap-3">
        <div class="filter-item">
          <span class="filter-label">用户昵称:</span>
          <el-input
              v-model="searchNickname"
              placeholder="请输入（模糊查询）"
              clearable
              size="default"
              style="width: 220px;"
          />
        </div>

        <div class="filter-item">
          <span class="filter-label">创建日期:</span>
          <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              size="default"
              style="width: 280px;"
              :shortcuts="dateShortcuts"
          />
        </div>

        <div class="filter-item ml-auto">
          <el-button type="primary" icon="Search" @click="handleSearch">
            查询
          </el-button>
          <el-button icon="Refresh" @click="resetSearch">
            重置
          </el-button>
        </div>
      </div>
    </div>

    <!-- 评论列表表格 -->
    <el-table
        v-loading="loading"
        :data="commentList"
        border
        style="width: 100%;"
        :row-style="{ height: '60px' }"
    >
      <el-table-column type="index" label="序号" width="60" align="center" />

      <!-- 头像列 -->
      <el-table-column label="头像" width="80" align="center">
        <template #default="{ row }">
          <el-avatar :size="40" :src="row.avatar">
            {{ row.nickname ? row.nickname.substring(0, 1) : '' }}
          </el-avatar>
        </template>
      </el-table-column>

      <el-table-column prop="nickname" label="昵称" width="120" />
      <el-table-column prop="content" label="评论内容" min-width="200" />
      <el-table-column prop="createTime" label="发布时间" width="180" />

      <!-- 状态列 -->
      <el-table-column prop="status" label="状态" width="80" align="center">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)" size="small">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="likeCount" label="点赞数" width="100" align="center" />
      <el-table-column label="操作" width="120" fixed="right" align="center">
        <template #default="{ row }">
          <div class="flex justify-center space-x-2">
            <el-tooltip content="删除" placement="top">
              <el-button
                  type="danger"
                  circle
                  size="small"
                  icon="Delete"
                  @click="handleDelete(row.id)"
              />
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="mt-4 flex justify-end">
      <el-pagination
          background
          layout="prev, pager, next, total"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'

// 模拟API调用（已移除示例数据，此处返回空数组）
const getCommentList = async (params) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 返回空的评论列表
      const mockData = {
        code: 200,
        data: {
          list: [],
          total: 0
        }
      }
      resolve(mockData)
    }, 500)
  })
}

// 模拟删除评论API
const deleteComment = async (id) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({ code: 200, message: '删除成功' })
    }, 300)
  })
}

// 搜索关键词（按用户昵称搜索）
const searchNickname = ref('')
// 日期范围
const dateRange = ref([])
// 日期快捷选项
const dateShortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '最近一个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '最近三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
// 列表数据
const commentList = ref([])
const loading = ref(false)

// 状态映射函数
const getStatusText = (status) => {
  switch (status) {
    case 0: return '待审核'
    case 1: return '正常'
    case 2: return '已屏蔽'
    default: return '未知'
  }
}
const getStatusType = (status) => {
  switch (status) {
    case 0: return 'warning'
    case 1: return 'success'
    case 2: return 'danger'
    default: return 'info'
  }
}

// 获取评论列表
const fetchData = async () => {
  try {
    loading.value = true
    const params = {
      nickname: searchNickname.value,  // 按昵称模糊查询
      startDate: dateRange.value && dateRange.value.length > 0 ? dateRange.value[0] : null,
      endDate: dateRange.value && dateRange.value.length > 0 ? dateRange.value[1] : null,
      page: currentPage.value,
      size: pageSize.value
    }
    const res = await getCommentList(params)
    if (res.data) {
      commentList.value = res.data.list || []
      total.value = res.data.total || 0
    }
  } catch (error) {
    console.error('获取评论列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 搜索操作
const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

// 重置搜索操作
const resetSearch = () => {
  searchNickname.value = ''
  dateRange.value = []
  currentPage.value = 1
  fetchData()
}

// 分页切换
const handlePageChange = (page) => {
  currentPage.value = page
  fetchData()
}

// 删除评论操作
const handleDelete = (id) => {
  ElMessageBox.confirm('确认删除该评论吗？', '提示', {
    type: 'warning'
  })
      .then(async () => {
        await deleteComment(id)
        ElMessage.success('删除成功')
        fetchData()
      })
      .catch(() => {
        // 用户取消操作
      })
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.comment-manage {
  padding: 0;
}

.filter-area {
  padding: 15px;
  border-bottom: 1px solid #ebeef5;
  background-color: #f8f9fa;
}

.filter-item {
  display: flex;
  align-items: center;
  margin-right: 10px;
}

.filter-label {
  margin-right: 8px;
  white-space: nowrap;
}

.flex {
  display: flex;
}

.items-center {
  align-items: center;
}

.justify-between {
  justify-content: space-between;
}

.justify-end {
  justify-content: flex-end;
}

.justify-center {
  justify-content: center;
}

.space-x-2 > * + * {
  margin-left: 8px;
}

.ml-auto {
  margin-left: auto;
}

.mb-5 {
  margin-bottom: 15px;
}

.mt-4 {
  margin-top: 16px;
}

.gap-3 {
  gap: 12px;
}

.flex-wrap {
  flex-wrap: wrap;
}
</style>
